package com.game.raceroom.events
{
	import flash.events.KeyboardEvent;
	
	import com.util.DEF;
	import com.game.raceroom.SkillBarCanvas;
	import com.game.raceroom.player.contrl.ActorContrl;
	
	import mx.containers.Canvas;
	public class GameCanvasKeyboardEvent
	{
		private var actorContrl:ActorContrl;
		private var run_id:int;
		private var stage:Canvas;
		private var skillBar:SkillBarCanvas;
		
		public function GameCanvasKeyboardEvent(stage:Canvas,actorContrl:ActorContrl,skb:SkillBarCanvas)
		{
			 this.stage=stage;
		     this.actorContrl=actorContrl;
		     skillBar=skb;
		     stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
             stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
		}
		public function destroy():void{
			 stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
		     stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
		}
        public   function keyDownHandler(evt:KeyboardEvent):void {
            // while(actorContrl.locked){};
        	 var code:int=evt.keyCode ;
    		 switch (code) {
      			 case DEF.KEY_UP:
      			 case DEF.KEY_W:
        		    actorContrl.onKeyPress(DEF.DIRECTION_UP);
        			break;
      			 case DEF.KEY_DOWN:
      			 case DEF.KEY_S:
        		    actorContrl.onKeyPress(DEF.DIRECTION_DOWN);
        			break;
        		 case DEF.KEY_LEFT:
      			 case DEF.KEY_A:
        		    actorContrl.onKeyPress(DEF.DIRECTION_LEFT);
        			break;
        		 case DEF.KEY_RIGHT:
      			 case DEF.KEY_D:
        		    actorContrl.onKeyPress(DEF.DIRECTION_RIGHT);
        		    break;
        		 case DEF.KEY_SPACE: 
        		    actorContrl.onKeyPress(DEF.DIRECTION_STOP);  
        			break;
        	     default :
        	        skillBar.pressHotkey(code);
        	        break;		
             }
        }		
 
        //----------------------------------------------------------
        public function keyUpHandler(evt:KeyboardEvent):void {
            // while(actorContrl.locked){};
        	 var code:int=evt.keyCode ;
    		 switch (code) {
      			 case DEF.KEY_UP:
      			 case DEF.KEY_W:
        			 actorContrl.onKeyRelease(DEF.DIRECTION_UP);
        			break;
      			 case DEF.KEY_DOWN:
      			 case DEF.KEY_S:
        			  actorContrl.onKeyRelease(DEF.DIRECTION_DOWN);
        			break;
        		 case DEF.KEY_LEFT:
      			 case DEF.KEY_A:
        			  actorContrl.onKeyRelease(DEF.DIRECTION_LEFT);
        			break;
        		 case DEF.KEY_RIGHT:
      			 case DEF.KEY_D:
        		     actorContrl.onKeyRelease(DEF.DIRECTION_RIGHT);
        		    break;
                 case DEF.KEY_SPACE: 
        		     actorContrl.onKeyRelease(DEF.DIRECTION_STOP); 
        			break;
             }
	    }
      
    }

}